package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"regexp"
	"sync"
	"strings"
	"strconv"
	"time"
)

var (
	reQQEmail = `(\d+)@qq.com`
	reImg=`https?://[^"]+?(\.((jpg)|(png)|(jpeg)|(gif)|(bmp)))`
    chanImageUrls chan string
	waitGroup sync.WaitGroup
	chanTask chan string
	
)

func GetEmail(){
	resp,err :=http.Get("https://tieba.baidu.com/p/6051076813?red_tag=1573533731")
	HandleError(err,"Http.get url")
	defer resp.Body.Close()
	pageBytes,err :=ioutil.ReadAll(resp.Body)
	HandleError(err,"ioutil.ReadAll")

	pageStr := string(pageBytes)
	fmt.Println(pageStr)
	re:=regexp.MustCompile(reQQEmail)

	results :=re.FindAllStringSubmatch(pageStr,-1)

	   for _,result :=range results {
		   fmt.Println("email:",result[0])
		   fmt.Println("qq:",result[1])
	   }

}


func HandleError(err error,why string){
	if err!=nil {
		fmt.Println(why,err)
	}
}

func main(){

    
}


func GetImgRoutine()
{
//1.初始化管道
 chanImageUrls =make(chan string,1000000)
 chanTask =make(chan string,26)
 //爬蟲攜程
 for i:=1;i<27;i++ {
	 waitGroup.Add(1)
	 go getImgUrls("https://www.bizhizu.cn/shouji/tag-%E5%8F%AF%E7%88%B1/" + strconv.Itoa(i) + ".html")
 }
 //任務統計攜程
 waitGroup.Add(1)
 go CheckOK()

 for i:=0;i<5;i++ {
	 waitGroup.Add(1)
	 go DownloadImg()
 }
 waitGroup.Wait()

}


func GetPageStr(url string)(pageStr string){
	resp,err:=http.Get(url)
	HandleError(err,"http.Get Url")
	defer resp.Body.Close()
	pageBytes,err:=ioutil.ReadAll(resp.Body)
	HandleError(err,"ioutil.ReadAll")
	pageStr =string(pageBytes)
	return pageStr
}


func DownloadFile(url string,filename string ) (ok bool) {
	resp,err:=http.Get(url)
	HandleError(err,"http.get.url")

	defer resp.Body.Close()

	bytes,err :=ioutil.ReadAll(resp.Body)
	HandleError(err,"resp.body")
	filename ="D:/Gocode/" + filename
    err =ioutil.WriteFile(filename,bytes,0666)
	if err!=nil {
      return false
	}else{
   return true
	}
}


func DownloadImg(){
	for url :=range chanImageUrls {
     filename :=GetFilenameFromUrl(url)
     ok:=DownloadFile(url,filename)
	 if ok {
		 fmt.Printf("%s 下載成功",filename)
	 }else {
		  fmt.Printf("%s 下載失敗",filename)
	 }
	}
	waitGroup.Done()
}


func GetFilenameFromUrl(url string) (filename string) {
	lastIndex :=strings.LastIndex(url,"/")
	filename =url[lastIndex+1:]
	timePrefix :=strconv.Itoa(int(time.Now().UnixNano()))
    filename=timePrefix+"_"+filename
	return
}


func CheckOK() {
	var count int
    for {
		url :=<-chanTask
		fmt.Printf("%s 完成了爬取任務\n",url)
		count++ 
		if count==26 {
			close(chanImageUrls)
			break
		}
	}
	waitGroup.Done() 
}


func getImgUrls(url string) {
	urls :=getImgs(url)
	for _,url :=range urls {
		chanImageUrls <-url
	}

	chanTask<-url
	waitGroup.Done()
}


func getImgs(url string) (urls []string) {
	pageStr :=GetPageStr(url)
	re :=regexp.MustCompile(reImg)
	results :=re.FindAllStringSubmatch(pageStr,-1)
	fmt.Printf("共找到%d條結果",len(results))
	for _,result:=range results {
		url :=result[0]
		urls=append(urls,url)
	}
	return
}